package main.java.com.chankudo.network;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * tcp sever端的一个示例
 */
public class SimpleTcpServer extends Thread{
    private ServerSocket serverSocket;
    private ExecutorService executorService;

    public SimpleTcpServer(int port) throws IOException{
        super.setName("Thread-server");
        serverSocket = new ServerSocket(port);
        executorService = Executors.newSingleThreadExecutor();
    }

    @Override
    public void run() {
        while(!isInterrupted()){
            try {
                Socket socket = serverSocket.accept();
                HandleRunnable taskRunnable = new HandleRunnable(socket);
                executorService.submit(taskRunnable);
            }
            catch(SocketException sExp){
                System.out.println("server shoutdown:");
                sExp.printStackTrace();
                break;
            }
            catch (IOException e) {
                System.out.println("accept error:");
                e.printStackTrace();
            }
        }
        executorService.shutdown();
        
        System.out.println("server close success!!!");
    }

    public ServerSocket getServerSocket() {
        return serverSocket;
    }

    public void setServerSocket(ServerSocket serverSocket) {
        this.serverSocket = serverSocket;
    }

    class HandleRunnable implements Runnable{
        Socket socket;
        HandleRunnable(Socket aSocket){
            this.socket = aSocket;
        }
        @Override
        public void run() {
            String str = null;
            BufferedReader reader = null;
            try (InputStream inputStream = socket.getInputStream()){
                // reader = new  BufferedReader(new InputStreamReader(inputStream, "UTF-8")) ;

                // str = reader.readLine();
                // while(!"n".equals(str)){
                //     System.out.println("server get string:"+str);
                //     str = reader.readLine();
                // }
                
                System.out.println("receive n. socket will close.");
                
                // if(str.isEmpty() ){
                //     System.out.println("receive empty. socket will close.");
                // }else if(str.equals("bye")){
                //     System.out.println("receive bye. server will close.");
                //     SimpleTcpServer.this.getServerSocket().close();
                // }  
            } 
            catch (IOException e) {
                System.out.println("read input stream error.");   
                e.printStackTrace();
            }
            finally{
                try {
                    socket.close();
                    SimpleTcpServer.this.getServerSocket().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
            }
        }
    
    }
    
}


